package com.mpcm.web.client.page.home;

import com.gwtplatform.mvp.client.ViewImpl;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Widget;
import com.google.inject.Inject;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.ui.AbsolutePanel;
import com.google.gwt.user.client.ui.Image;
import com.mpcm.web.shared.SharedConstants;

public class HomeView extends ViewImpl implements HomePresenter.MyView {

	private final int image1_left = 500;
	private final int image1_top = 100;
	private final int image2_left = 500;
	private final int image2_top = 100;
	
	private final Widget widget;

	public interface Binder extends UiBinder<Widget, HomeView> {
	}
	
	private int prevCalTop = 0;
	private int bgLeft = 0;
	
	@UiField AbsolutePanel container;
	@UiField Image imageBackground;
	@UiField Image image1;
	@UiField Image image2;

	@Inject
	public HomeView(final Binder binder) {
		widget = binder.createAndBindUi(this);
	}

	@Override
	public Widget asWidget() {
		return widget;
	}
	
	@Override
	public void setContainerWidth(int clientWidth) {
		// set container (absolute panel) width
		container.setWidth(Window.getClientWidth() + "px");		
	}
	
	@Override
	public void setObjectsPosition(int clientWidth) {
		int calTop = (int)((-container.getElement().getAbsoluteTop()) * 0.8);
		
		if(calTop == prevCalTop)
			return;
		
		if(calTop > prevCalTop + (SharedConstants.MOVE_GRAIN_SLOW - 1))
			prevCalTop += SharedConstants.MOVE_GRAIN_SLOW;
		else if(calTop < prevCalTop - (SharedConstants.MOVE_GRAIN_SLOW - 1))
			prevCalTop -= SharedConstants.MOVE_GRAIN_SLOW;
		
		// set background position
		bgLeft = (clientWidth - SharedConstants.BGIMAGE_WIDTH) / 2;
		container.setWidgetPosition(imageBackground, bgLeft, prevCalTop);
	}
}
